﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using sharp_net;
using sharp_net.Repositories;
using sharp_net.Specifications;
using zkdao.Domain;
using zkdao.Enum;

namespace zkdao.Application {

    public class TagApplication : BaseApplication, ITagService {

        public TagData Get(Guid ID) {
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var tagRepository = context.GetRepository<Tag>();
                var tag = tagRepository.Find(Specification<Tag>.Eval(c => c.Id == ID));
                if (tag == null)
                    return null;
                var tagData = Mapper.Map<Tag, TagData>(tag);
                return tagData;
            }
        }

        public TagData GetByName(string name) {
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var tagRepository = context.GetRepository<Tag>();
                var tag = tagRepository.Find(Specification<Tag>.Eval(c => c.Name == name));
                if (tag == null)
                    return null;
                var tagData = Mapper.Map<Tag, TagData>(tag);
                return tagData;
            }
        }

        public IList<TagData> GetList() {
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var tagRepository = context.GetRepository<Tag>();
                var tags = tagRepository.FindAll();
                if (tags == null)
                    return null;
                var tagDatas = new List<TagData>();
                foreach (var user in tags) {
                    tagDatas.Add(Mapper.Map<Tag, TagData>(user));
                }
                return tagDatas;
            }
        }

        public IList<TagData> GetListByGroup(eTagGroup group) {
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var tagRepository = context.GetRepository<Tag>();
                var tags = tagRepository.FindAll(Specification<Tag>.Eval(t=>t.GroupEnum == (int)group));
                if (tags == null)
                    return null;
                var tagDatas = new List<TagData>();
                foreach (var user in tags) {
                    tagDatas.Add(Mapper.Map<Tag, TagData>(user));
                }
                return tagDatas;
            }
        }

        public TagData Creat(TagData dataObject) {
            if (dataObject == null)
                throw new ArgumentNullException("tagDataObject");
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var tagRepository = context.GetRepository<Tag>();
                Tag tag = Mapper.Map<TagData, Tag>(dataObject);
                tag.Creat();
                tagRepository.Add(tag);
                context.Commit();
                return Mapper.Map<Tag, TagData>(tag);
            }
        }

        public void Update(TagData dataObject) {
            if (string.IsNullOrEmpty(dataObject.ID))
                throw new ArgumentNullException("ID");
            Tag replyChild = Mapper.Map<TagData, Tag>(dataObject);
            using (var context = IocLocator.Instance.GetImple<IRepositoryContext>()) {
                var tagRepository = context.GetRepository<Tag>();
                var upTag = tagRepository.Get(Specification<Tag>.Eval(c => c.Id.ToString() == dataObject.ID));
                upTag.Update(dataObject);
                //if (dataObject.Rebates != null && dataObject.Rebates.Count != 0) {
                //    var rebateRepository = context.GetRepository<Rebate>();
                //    foreach (var item in dataObject.Rebates) {
                //        var rebate = rebateRepository.GetById(item.ID);
                //        upTag.Rebates.Add(rebate);
                //    }
                //}
                //if (dataObject.Infos != null && dataObject.Infos.Count != 0) {
                //    var infoRepository = context.GetRepository<Info>();
                //    foreach (var item in dataObject.Rebates) {
                //        var info = infoRepository.GetById(item.ID);
                //        upTag.Infos.Add(info);
                //    }
                //}
                tagRepository.Update(upTag);
            }
        }
    }
}